(function(){"use strict";BX.namespace("BX.Landing.UI.Field");var t=BX.Landing.Utils.addClass;var e=BX.Landing.Utils.removeClass;var i=BX.Landing.Utils.hasClass;var n=BX.Landing.Utils.isBoolean;var a=BX.Landing.Utils.isArray;var s=BX.Landing.Utils.create;var o=BX.Landing.Utils.append;var r=BX.Landing.Utils.insertAfter;var l=BX.Landing.Utils.insertBefore;var h=BX.Landing.Utils.attr;var d=BX.Landing.Utils.slice;var u=BX.Landing.Utils.style;var p=BX.Landing.Utils.bind;var c=BX.Landing.Utils.unbind;var g=BX.Landing.Utils.data;var v=BX.Landing.Utils.remove;var m=BX.Landing.Utils.nextSibling;var f=BX.Landing.Utils.prevSibling;var L=BX.Landing.Utils.escapeHtml;var B=BX.Landing.Utils.findParent;var A=BX.Landing.Utils.offsetTop;var D=BX.Landing.Utils.offsetLeft;BX.Landing.UI.Field.DragAndDropList=function(e){BX.Landing.UI.Field.BaseField.apply(this,arguments);t(this.layout,"landing-ui-field-dnd-list");this.isMultiple=n(e.multiple)?e.multiple:false;this.items=a(e.items)?e.items:[];this.value=a(e.value)?e.value:[];this.onDragStart=this.onDragStart.bind(this);this.onDragEnd=this.onDragEnd.bind(this);this.onDragOver=this.onDragOver.bind(this);this.onItemDragOver=this.onItemDragOver.bind(this);this.onItemDragLeave=this.onItemDragLeave.bind(this);this.onDragLeave=this.onDragLeave.bind(this);this.onDrop=this.onDrop.bind(this);this.onDrag=this.onDrag.bind(this);this.onRemoveClick=this.onRemoveClick.bind(this);this.onElementClick=this.onElementClick.bind(this);this.onMouseWheel=this.onMouseWheel.bind(this);this.onMouseOver=this.onMouseOver.bind(this);this.onMouseLeave=this.onMouseLeave.bind(this);if(typeof window.onwheel!=="undefined"){this.wheelEventName="wheel"}else if(typeof window.onmousewheel!=="undefined"){this.wheelEventName="mousewheel"}this.target=s("div",{props:{className:"landing-ui-field-dnd-target"},children:[s("div",{props:{className:"landing-ui-field-dnd-target-browser-header"},children:[s("span"),s("span"),s("span")]})]});this.addButton=new BX.Landing.UI.Button.BaseButton("add_catalog_item",{onClick:this.onAddItemClick.bind(this),className:"landing-ui-field-dnd-add-button"});this.valueArea=s("div",{props:{classList:"landing-ui-field-dnd-list-value"}});this.itemsArea=s("div",{props:{classList:"landing-ui-field-dnd-list-items"}});o(this.valueArea,this.target);o(this.target,this.input);o(this.addButton.layout,this.target);p(this.valueArea,"dragover",this.onDragOver);p(this.valueArea,"dragleave",this.onDragLeave);p(this.valueArea,"drop",this.onDrop);this.items.forEach(function(t){o(this.createItem(t),this.itemsArea)},this);this.value.forEach(function(t){var e=this.getItem(t);if(e){o(this.createItem(e),this.valueArea)}},this);this.adjustPlaceholder();this.value=this.getValue()};BX.Landing.UI.Field.DragAndDropList.prototype={constructor:BX.Landing.UI.Field.DragAndDropList,__proto__:BX.Landing.UI.Field.BaseField.prototype,onDragStart:function(e){this.dragItem=e.currentTarget;this.dragIndex=d(this.valueArea.children).findIndex(function(t){return t===e.currentTarget});this.valueAreaRect=this.valueArea.getBoundingClientRect();t(this.dragItem,"landing-ui-ondrag");t(this.valueArea,"landing-ui-ondrag")},onDragEnd:function(){e(this.valueArea,"landing-ui-over");e(this.dragItem,"landing-ui-ondrag");e(this.valueArea,"landing-ui-ondrag");d(this.valueArea.children).forEach(function(t){u(t,null)});this.cloned=null;this.dragItem=null;if(this.isChanged()){this.onValueChangeHandler(this);this.value=this.getValue()}},onDragOver:function(e){e.preventDefault();e.dataTransfer.dropEffect="copy";t(e.currentTarget,"landing-ui-over")},onItemDragOver:function(t){var e=t.currentTarget;if(this.itemsArea.contains(this.dragItem)||this.valueArea.contains(this.dragItem)){if(this.valueArea.contains(e)&&e!==this.cloned){var i=e.getBoundingClientRect();var n=i.top+i.height/2;if(!this.cloned&&!this.valueArea.contains(this.dragItem)){this.cloned=this.createItemFromElement(this.dragItem);o(this.cloned,this.valueArea)}if(!this.cloned&&this.valueArea.contains(this.dragItem)){this.cloned=this.dragItem}if(t.clientY>n&&m(e)!==this.cloned){return r(this.cloned,e)}if(t.clientY<n&&f(e)!==this.cloned){return l(this.cloned,e)}}}this.adjustPopupPosition()},onAddItemClick:function(t){t.preventDefault();t.stopPropagation();if(!this.popup){this.popup=new BX.Main.Popup({id:"catalog_blocks_list",bindElement:this.addButton.layout,content:this.itemsArea,autoHide:true,height:176});this.popup.contentContainer.style.overflowX="hidden";p(this.popup.popupContainer,"mouseover",this.onMouseOver);p(this.popup.popupContainer,"mouseleave",this.onMouseLeave);var e=BX.Landing.PageObject.getRootWindow();p(e.document,"click",this.onDocumentClick.bind(this));o(this.popup.popupContainer,B(this.addButton.layout,{className:"landing-ui-panel-content-body-content"}))}if(this.popup.isShown()){this.popup.close()}else{this.popup.show()}this.adjustPopupPosition()},onMouseOver:function(){p(this.popup.popupContainer,this.wheelEventName,this.onMouseWheel);p(this.popup.popupContainer,"touchmove",this.onMouseWheel)},onMouseLeave:function(){c(this.popup.popupContainer,this.wheelEventName,this.onMouseWheel);c(this.popup.popupContainer,"touchmove",this.onMouseWheel)},onMouseWheel:function(t){t.stopPropagation();t.preventDefault();var e=BX.Landing.UI.Panel.Content.getDeltaFromEvent(t);var i=this.popup.contentContainer.scrollTop;requestAnimationFrame(function(){this.popup.contentContainer.scrollTop=i-e.y}.bind(this))},adjustPopupPosition:function(){if(this.popup){requestAnimationFrame(function(){var t=B(this.addButton.layout,{className:"landing-ui-panel-content-body-content"});var e=A(this.addButton.layout,t);var i=D(this.addButton.layout,t);var n=this.addButton.layout.getBoundingClientRect();var a=this.popup.popupContainer.getBoundingClientRect();var s=14;this.popup.popupContainer.style.top=e+n.height+s+"px";this.popup.popupContainer.style.left=i-a.width/2+n.width/2+"px";this.popup.setAngle({offset:71,position:"top"})}.bind(this))}},createItemFromElement:function(t){var e=t.querySelector("img");var i={name:t.innerText,value:g(t,"value")};if(e){i.image=e.src}return this.createItem(i)},onDragLeave:function(t){e(t.currentTarget,"landing-ui-over")},onItemDragLeave:function(t){t.stopPropagation()},onDrop:function(t){t.stopPropagation();if(this.itemsArea.contains(this.dragItem)||this.valueArea.contains(this.dragItem)){if(!this.valueArea.contains(this.dragItem)&&!this.valueArea.contains(this.cloned)){var e=this.createItemFromElement(this.dragItem);o(e,this.valueArea);this.cloned=null;this.adjustPlaceholder();this.adjustPopupPosition()}}if(this.isChanged()){this.onValueChangeHandler(this);this.value=this.getValue()}},onDrag:function(t){if(this.cloned&&this.valueArea.contains(this.cloned)){if(t.clientX>this.valueAreaRect.right){v(this.cloned);this.cloned=null;this.adjustPlaceholder();this.adjustPopupPosition()}}},onRemoveClick:function(t){t.preventDefault();t.stopPropagation();v(t.currentTarget.parentNode);this.adjustPlaceholder();this.adjustPopupPosition();if(this.isChanged()){this.onValueChangeHandler(this);this.value=this.getValue()}},onElementClick:function(t){t.preventDefault();t.stopPropagation();if(this.itemsArea.contains(t.currentTarget)){var e=this.createItemFromElement(t.currentTarget);o(e,this.valueArea);this.adjustPlaceholder();this.adjustPopupPosition()}if(this.isChanged()){this.onValueChangeHandler(this);this.value=this.getValue()}},getSelectedItem:function(){return d(this.valueArea.children).find(function(t){return i(t,"landing-ui-selected")})||d(this.itemsArea.children).find(function(t){return i(t,"landing-ui-selected")})},onDocumentClick:function(){if(this.popup){this.popup.close()}},adjustPlaceholder:function(){if(d(this.valueArea.children).length===0){this.placeholder=this.createValuePlaceholder();o(this.placeholder,this.valueArea)}else if(this.placeholder){v(this.placeholder);this.placeholder=null}},getItem:function(t){return this.items.find(function(e){return e.value==t})},createValuePlaceholder:function(){return s("div",{props:{className:"landing-ui-field-dnd-value-placeholder"},children:[s("span",{html:BX.Landing.Loc.getMessage("LANDING_FIELD_CATALOG_CONSTRUCTOR_PLACEHOLDER_TEXT")})]})},createItem:function(t){var e=s("div",{props:{className:"landing-ui-field-dnd-list-item"+(t.image?" landing-ui-with-image":"")},attrs:{title:t.name},html:t.image?s("img",{props:{src:t.image}}).outerHTML:L(t.name)});var i=s("span",{props:{className:"landing-ui-field-dnd-list-item-remove"}});o(i,e);g(e,"data-value",t.value);h(e,"draggable","true");p(e,"dragstart",this.onDragStart);p(e,"dragend",this.onDragEnd);p(e,"dragover",this.onItemDragOver);p(e,"dragleave",this.onItemDragLeave);p(e,"drag",this.onDrag);p(i,"click",this.onRemoveClick);p(e,"click",this.onElementClick);return e},isChanged:function(){return JSON.stringify(this.value)!==JSON.stringify(this.getValue())},getValue:function(){var t=d(this.valueArea.children);t=t.filter(function(t){return i(t,"landing-ui-field-dnd-list-item")});return t.map(function(t){return g(t,"data-value")})}}})();
//# sourceMappingURL=dnd_list.map.js